package com.skit.main.ui.add

import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AlertDialog
import com.skit.common.base.BaseActivity
import com.skit.common.bean.ActivityBean
import com.skit.common.http.HttpUtils
import com.skit.common.utils.ToastUtils
import com.skit.main.R
import com.skit.common.retrofit.ActivityProtocol
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.android.synthetic.main.main_activity_type.*
import retrofit2.Retrofit
import javax.inject.Inject

/**
 * @author shuike <a href="mailto:shuike007@gmail.com">Contact me.</a>
 * @version 1.0
 * @since  2020/6/3
 */

@AndroidEntryPoint
class ActivityTypeActivity : BaseActivity() {
    private var adminScoreNum: Double = 0.3
    private var joinScoreNum: Double = 0.3
    private val list: MutableList<ActivityBean.TypeBean> = ArrayList()
    private var typeIndex = 0
    private var activityTypeBean: ActivityBean.TypeBean? = null

    @Inject
    lateinit var retrofit: Retrofit

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity_type)

        iniViews()

    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            if (activityTypeBean != null) {
                val intent = Intent()
                intent.putExtra("typeBean", activityTypeBean)
                intent.putExtra("boutique", boutiqueSwitch.isChecked)
                intent.putExtra("stuScore", joinScoreNum)
                intent.putExtra("adminScore", adminScoreNum)
                setResult(AddActivity.TYPE_ACTIVITY, intent)
                finish()
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

    private fun iniViews() {
        activityTypeTV.text =
            resources.getString(R.string.main_activity_type, "未选择")
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        activityType.setOnClickListener {
            getTypeData()
        }

        boutique.setOnClickListener {
            boutiqueSwitch.isChecked = !boutiqueSwitch.isChecked
        }

        adminScoreTV.text =
            resources.getString(
                R.string.admin_score, adminScoreNum
            )

        adminScore.setOnClickListener {
            val scopeArray =
                if (!boutiqueSwitch.isChecked) {
                    arrayOf("0.3", "0.5", "1.0")
                } else {
                    arrayOf("0.5", "1.0", "1.5")
                }
            val builder: AlertDialog.Builder =
                AlertDialog.Builder(this)
                    .setTitle("活动类型").setItems(scopeArray) { dialog, which ->
                        run {
                            adminScoreNum =
                                scopeArray[which].toDouble()
                            adminScoreTV.text =
                                resources.getString(
                                    R.string.admin_score, adminScoreNum
                                )
                        }
                    }
            builder.show()
        }

        stuScoreTV.text =
            resources.getString(
                R.string.join_score, joinScoreNum
            )

        stuScore.setOnClickListener {
            val scopeArray =
                if (!boutiqueSwitch.isChecked) {
                    arrayOf("0.3", "0.5", "1.0")
                } else {
                    arrayOf("0.5", "1.0", "1.5")
                }
            val builder: AlertDialog.Builder =
                AlertDialog.Builder(this)
                    .setTitle("活动类型").setItems(scopeArray) { dialog, which ->
                        run {
                            joinScoreNum =
                                scopeArray[which].toDouble()
                            stuScoreTV.text =
                                resources.getString(
                                    R.string.join_score, joinScoreNum
                                )
                        }
                    }
            builder.show()
        }
    }

    private fun typesDialog() {
        val scopeArray = arrayOfNulls<String>(list.size)
        for (i in list.indices) {
            scopeArray[i] = list[i].name
        }
        val builder: AlertDialog.Builder =
            AlertDialog.Builder(this)
                .setTitle("活动类型").setItems(scopeArray) { dialog, which ->
                    run {
                        typeIndex = which
                        activityTypeBean = list[which]
                        activityTypeTV.text =
                            resources.getString(R.string.main_activity_type, scopeArray[typeIndex])
                    }
                }
        builder.show()
    }

    private fun getTypeData() {
//        val activityProtocol = RetrofitClient.getService(ActivityProtocol::class.java)
        val activityProtocol = retrofit.create(ActivityProtocol::class.java)
        val activityType = activityProtocol.activityType
        HttpUtils.doRequest(activityType) {
            if (it == null || !it.isSuccess()) {
                if (it != null) ToastUtils.showToast(it.msg)
                return@doRequest
            }
            list.clear()
            list.addAll(it.data)
            typesDialog()
        }
    }
}